﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Phatent.Model.Pages;
namespace Phatent.Command.Pages
{
    public class CaseCommand
    {
        public static CaseCollection Gets(CaseQuery sea)
        {
            var ret = new CaseCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<Case> q = db.DataContext.Cases;
                if (sea != null)
                {
                    if (sea.PID > 0 | sea.PID == null)
                        q = q.Where(p => p.PID == sea.PID);
                    if (sea.OnlySubCase)
                        q = q.Where(p => p.PID != null);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q =q.OrderBy(p=>p.PID).ThenBy(p => p.Pindex).ThenByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<CaseModel, Case, CaseCollection>(lst);
                    ret.TotalRecordsCount = n;
                    if (sea != null)
                    {
                        if (sea.GetItems)
                        {
                            foreach (var it in ret)
                            {
                                var d = lst.Find(p => p.ID == it.ID);
                                it.Items = Convertor.ConvertCollection<CaseModel, Case, CaseCollection>(d.Cases1.OrderBy(p => p.Pindex).ToList());
                            }
                        }
                        if (sea.GetParent)
                        {
                            foreach (var it in ret)
                            {
                                var d = lst.Find(p => p.ID == it.ID);
                                it.ParentCase = Convertor.ConvertTo<CaseModel, Case>(d.Case1);
                            }
                        }
                    }
                }
            }
            return ret;
        }

        public static CaseModel GetTopCaseByFriendUrl(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                using (DBCentre db = new DBCentre())
                {
                    var dCase = db.DataContext.Cases.FirstOrDefault(p => p.FriendUrl == url);
                    if (dCase == null)
                    {
                        var dPage = db.DataContext.Pages.FirstOrDefault(p => p.FriendUrl == url);
                        if (dPage != null)
                            dCase = dPage.Case;
                    }
                    if (dCase != null)
                    {
                        var curID = dCase.ID;
                        if (dCase.PID != null)
                            dCase = dCase.Case1;
                        var ret = Convertor.ConvertTo<CaseModel, Case>(dCase);
                        ret.Items = Convertor.ConvertCollection<CaseModel, Case, CaseCollection>(dCase.Cases1.OrderBy(p=>p.Pindex).ToList());
                        ret.CurrentID = curID;
                        return ret;
                    }
                }
            }
            return null;
        }

        public static CaseModel GetCaseByFriendUrl(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                using (DBCentre db = new DBCentre())
                {
                    var dCase = db.DataContext.Cases.FirstOrDefault(p => p.FriendUrl == url);
                    if (dCase == null)
                    {
                        var dPage = db.DataContext.Pages.FirstOrDefault(p => p.FriendUrl == url);
                        if (dPage != null)
                            dCase = dPage.Case;
                    }
                    if (dCase != null)
                    {
                        var ret = Convertor.ConvertTo<CaseModel, Case>(dCase);
                        if (dCase.PID != null)
                            ret.ParentCase = Convertor.ConvertTo<CaseModel, Case>(dCase.Case1);
                        return ret;
                    }
                }
            }
            return null;
        }

        public static CaseModel GetCaseByParent(CaseModel pcase)
        {
            if (pcase!=null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dCase = db.DataContext.Cases.FirstOrDefault(p => p.PID==pcase.ID);
                    if (dCase != null)
                    {
                        var ret = Convertor.ConvertTo<CaseModel, Case>(dCase);
                        ret.ParentCase = pcase;
                        return ret;
                    }
                }
            }
            return null;
        }

        public static CaseModel GetCaseByID(int cid)
        {
            if (cid>0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dCase = db.DataContext.Cases.FirstOrDefault(p => p.ID==cid);
                    if (dCase != null)
                    {
                        var ret = Convertor.ConvertTo<CaseModel, Case>(dCase);
                        if (dCase.PID > 0)
                            ret.ParentCase = Convertor.ConvertTo<CaseModel, Case>(dCase.Case1);
                        return ret;
                    }
                }
            }
            return null;
        }
    }
}
